package user;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DataBaseServlet;

/**
 * Servlet implementation class DeviceData
 */
@WebServlet(description = "拉取用户设备会话信息", urlPatterns = { "/DeviceData" })
public class DeviceData extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeviceData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final String DEFAULT = "{\"suc\":false}";
		
		String id = request.getParameter("id");
		System.out.println(id);
		
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		if(!id.equals("17121899"))
		{
			out.write(DEFAULT);
			return;
		}
		int deviceId = 0;
		try
		{
			System.out.println(request.getParameter("deviceId"));
			deviceId = Integer.parseInt(request.getParameter("deviceId"));
		}
		catch (NumberFormatException e)
		{
			System.out.println("无效的设备Id");
			out.write(DEFAULT);
			return;
		}
		
		
		DataBaseServlet db = DataBaseServlet.DB; // 数据库
		
		String res = db.queryDeviceInfo(deviceId);
		// System.out.println(res);
		out.write(res);
		
	}

}
